// pages/goods/goodsAccredit/goodsAccredit.js
const app = getApp()
import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    channel: '',
    goodsData: [],              // 商品瀑布流数据
    requestFlag: false,         // 加载瀑布流的请求标识
    page: 1,                    // 初始加载第一页数据
    noMore: false,
    brandId: '',
    sellerUid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
    let brandId = options.brandId
    let sellerUid = options.sellerUid

    this.data.channel = channel
    this.data.brandId = brandId
    this.data.sellerUid = sellerUid

    // 请求 热门商品 数据
    this.getGoodsData(this.data.page, channel)

    let systemInfo = {}

    try {
      systemInfo = wx.getSystemInfoSync()
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      winWidth: systemInfo.windowWidth,
      winHeight: systemInfo.windowHeight,
    })
  },

  /**
    * 事件处理
    * 热门商品上拉加载更多
    */
  scrolltolower: function (e) {
    this.getGoodsData(this.data.page, this.data.channel)
  },

  /**
    * 获取商品数据
    */
  getGoodsData: function (page, channel) {
    if (this.data.noMore) return false

    if (this.data.requestFlag) return false
    this.setData({
      requestFlag: true,
    })

    let count = 20;
    let start = (page - 1) * count;
    app.request({
      url: requestApi.goodsUrl,
      login: false,
      data: {
        start: start,
        count: count,
        channel: channel,
        brand_id: this.data.brandId,
        seller_uid: this.data.sellerUid,
      },
    }, (data) => {
      let goodsData, currentData, noMore, page_count

      currentData = this.data.goodsData
      goodsData = currentData.concat(data.original.data.goods.goods_list)
      goodsData = this.formatData(goodsData)
      console.log(goodsData)
      page_count = data.original.data.goods.goods_list.length ? data.original.data.goods.goods_list.length : 0

      if (page_count < count) {
        noMore = true
      } else {
        noMore = false
      }

      this.setData({
        goodsData: goodsData,
        loading: true,
        page: this.data.page + 1,
        noMore: noMore,
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      this.setData({
        requestFlag: false,
      })
    })
  },

  didClickGoodsCard: function (e) {
    var id = e.currentTarget.id
    let url = '../goodsDetail/goodsDetail?gid=' + id + '&channel=' + this.data.channel
    wx.navigateTo({
      url: url,
    })
  },

  // 处理请求返回的数据
  formatData: function (data) {
    if (data.length) {
      for (let i = 0, len = data.length; i < len; i++) {
        if (data[i].product_cover_image && data[i].product_cover_image.path) {
          data[i].product_cover_image.new_path = formatImage.cdnImage(data[i].product_cover_image.path, formatImage.THUMBNAIL_S)
        }

        if (data[i].goods_status_icon && data[i].goods_status_icon.path) {
          data[i].goods_status_icon.full_path = formatImage.cdnImage(data[i].goods_status_icon.path, formatImage.THUMBNAIL_S)
        }

        data[i].new_goods_price = data[i].goods_price ? formatPrice(data[i].goods_price / 100) : ''
        data[i].original_price = data[i].original_price ? formatPrice(data[i].original_price / 100) : ''
        data[i].goods_price = data[i].goods_price ? formatPrice(data[i].goods_price / 100) : ''
      }
    }

    return data
  },
})